﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Tpl.EfMigration.Migrations
{
    /// <inheritdoc />
    public partial class init1 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "author",
                columns: table => new
                {
                    id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    birthday = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_author", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "user",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    birthday = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "book",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    name = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
                    price = table.Column<double>(type: "double precision", nullable: false),
                    publisher = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    sn = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    authorid = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_book", x => x.id);
                    table.ForeignKey(
                        name: "FK_book_author_authorid",
                        column: x => x.authorid,
                        principalTable: "author",
                        principalColumn: "id");
                });

            migrationBuilder.CreateTable(
                name: "registration",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    userid = table.Column<long>(type: "bigint", nullable: false),
                    slsj = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
                    comment = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_registration", x => x.id);
                    table.ForeignKey(
                        name: "FK_registration_user_userid",
                        column: x => x.userid,
                        principalTable: "user",
                        principalColumn: "id");
                });

            migrationBuilder.CreateTable(
                name: "registrationdetail",
                columns: table => new
                {
                    id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    registrationid = table.Column<long>(type: "bigint", nullable: false),
                    bookid = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_registrationdetail", x => x.id);
                    table.ForeignKey(
                        name: "FK_registrationdetail_book_bookid",
                        column: x => x.bookid,
                        principalTable: "book",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_registrationdetail_registration_registrationid",
                        column: x => x.registrationid,
                        principalTable: "registration",
                        principalColumn: "id");
                });

            migrationBuilder.InsertData(
                table: "author",
                columns: new[] { "id", "birthday", "name" },
                values: new object[] { "090213204", new DateTime(1996, 8, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), "Bruce" });

            migrationBuilder.InsertData(
                table: "user",
                columns: new[] { "id", "birthday", "name" },
                values: new object[] { 1L, new DateTime(1996, 8, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), "Bruce" });

            migrationBuilder.CreateIndex(
                name: "IX_book_authorid",
                table: "book",
                column: "authorid");

            migrationBuilder.CreateIndex(
                name: "IX_book_sn",
                table: "book",
                column: "sn",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_registration_userid",
                table: "registration",
                column: "userid");

            migrationBuilder.CreateIndex(
                name: "IX_registrationdetail_bookid",
                table: "registrationdetail",
                column: "bookid");

            migrationBuilder.CreateIndex(
                name: "IX_registrationdetail_registrationid",
                table: "registrationdetail",
                column: "registrationid");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "registrationdetail");

            migrationBuilder.DropTable(
                name: "book");

            migrationBuilder.DropTable(
                name: "registration");

            migrationBuilder.DropTable(
                name: "author");

            migrationBuilder.DropTable(
                name: "user");
        }
    }
}
